/*
 * Copyright 2008 Slava Chernyak
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package com.chernyak.DataAdapter.types;

import java.util.List;
import java.util.Map;

import com.chernyak.DataAdapter.CompilerException;
import com.chernyak.DataAdapter.GlobalSymbolProperties;
import com.chernyak.DataAdapter.ParserNode;

public abstract class TypeDecl extends ParserNode {
	protected String name;
	protected boolean compare;
	
	public boolean isCompare() {
		return compare;
	}
	
	public String getName() {
		return name;
	}
	
	public TypeDecl(String name, boolean compare, int ln) {
		super(ln);
		this.name = name;
		this.compare = compare;
	}
	
	@Override
	public void typecheck(Map<String, GlobalSymbolProperties> globalSymbolTable,
			List<String> parrentSymbolTable, String packageContext) {
		for (String str : parrentSymbolTable) {
			if (name.equals(str)) {
				throw new CompilerException("Error while typechecking. \n" + 
						"Field Name " + name + " already in use.\nLine " + 
						ln + "\n", ln);
			}
		}
		parrentSymbolTable.add(name);
	}
	
	// arrayability functions
	protected abstract void lowerRead(StringBuilder sink, int arrayDim);
	protected abstract void lowerWrite(StringBuilder sink, int arrayDim);
	protected abstract void lowerToString(StringBuilder sink, int arrayDim);
	
	protected abstract String getTypeName();
	
	protected String genArrayDeref(int dim) {
		String arrayDeref = "";
		for (int i = 0; i < dim; i++ ) {
			arrayDeref += "[__da_ad_" + i + "]";
		}
		return arrayDeref;
	}

}
